/****************************************************************************
**
** Copyright (C) .  All rights reserved. 
**
****************************************************************************/
/*!
 * \file
 * Defines structures related to AVS file format and User data of AVI file
 */
#ifndef DEF_ARCHIVE_H
#define DEF_ARCHIVE_H

#pragma pack(1)

//==========================================================================================//
//																							//
// Time Index File		: Do not modify														//
//																							//
//==========================================================================================//

typedef struct _TIME_INDEX_HEADER
{
	WORD	wSign;
	WORD	wVersion;
	DWORD	dwFirstIndex;
	DWORD	dwNextIndex;
} TIME_INDEX_HEADER, *PTIME_INDEX_HEADER;

typedef struct _TIME_INDEX
{
	DWORD	dwRecTime;
	DWORD	dwRecMicroSec;
	DWORD	dwFileID;
	__int64	nStreamPos;
	DWORD	dwChannelFlag;
	DWORD	dwChannelSize[16];
} TIME_INDEX, *PTIME_INDEX;

//==========================================================================================//
//																							//
// I Frame Index File		: Do not modify													//
//																							//
//==========================================================================================//

typedef struct _IFRAMEINDEXHEADER		//12 bytes
{
	WORD    nSign;
	WORD    nVersion;
	DWORD   nFirstIndex;
	DWORD   nNextIndex;
} IFRAMEINDEXHEADER, *PIFRAMEINDEXHEADER;



typedef struct _IFRAMEINDEX //      size 20 bytes
{
	DWORD   nRecTime;
	DWORD   nFileID;
	__int64   nStreamPos;
	DWORD   nChannel;
} IFRAMEINDEX, *PIFRAMEINDEX;


//==========================================================================================//
//																							//
// User data																				//
//																							//
//==========================================================================================//

/*!
 * \brief user format structure, AVI file only
 *
 * User format structure
 */
typedef struct _TAG_USER_FORMAT
{
	DWORD dwVersion;		/**< Version - currently not used	*/
	/*!
	 * Event type
	 * 
	 *	Normal : 0x0
	 *	Event : 0x1(G-sensor event)
	 *	Parking : 0x2
	 *	Manual : 0x4
	 */
	DWORD dwEventType;
	DWORD dwReserved[14];	/**< reserved 56 bytes 	*/
} ST_USER_FORMAT, *LPST_USER_FORMAT;

#define _EX_HEADER_INFO
#undef _EX_HEADER_INFO

/*!
 * \brief AVI - user data structure, AV2 - stream header
 *
 * This structure is a stream header
 * AVI : user data structure
 * AV2 : stream header but user do not need to know about it
 */
typedef  struct _PICTURE_HEADER_EX
{
	DWORD   startcode;  			//!< must set to 0x000001FF

	DWORD   version			:24;	//!< FSH Version, this value must be 0x03260A
	DWORD   ch_seq			:8;		//!<	each channel's sequence number( 0 ~ 255 ): not used

	DWORD   v_ch_id_l       :4;    	//!< video channel number
	DWORD   v_ch_id_r       :4;    	//!< not used
	/*!
	 * Resolution code
	 * @see _TAG_RESOLUTION
	 */
	DWORD   v_res           :4;    	//!< Resolution Code
	DWORD   fshlength       :20;    //!<	FSH size + video length + audio length, max 1 mega

	DWORD   time;					//!<	A time when video stream was recording.

	DWORD   prev_fshlength  :20;    //!< A previous FSH length, not used in application
	DWORD   fld_type        :1;		//!<	0:odd, 1:even
	DWORD   codec_type      :2; 	//!<	0:JPEG, 1:H.264, 2:MPEG
	DWORD   v_format        :1;    	//!<	0:NTSC, 1:PAL, not used in this application
	DWORD   pre_skip_cnt    :8;		//!< Not used in this application

	DWORD   v_length        :19;    //!<	Video stream length
	DWORD   a_length        :13;    //!<	Audio data length

	DWORD   audio_link_l    :16;	//!< not used
	DWORD   msec    		:16;	//!<	A millisecond when video stream was recording.

	DWORD   vop_type     	:2; 	//!<	0:P-VOP, 1:I-VOP, 2:B-VOP
	DWORD   fps				:5;		//!<	fps of current video channel( 0 ~ 30 ). not used in this application
	DWORD	ncd				:1;		//!< not used in this application
	DWORD   blank_l         :1;		//!<	if current frame is a blank, it will be set to 1. not used in this application
	DWORD   blank_r         :1;		//!<	if it is blank because of video loss, not used in this application
	//    it will be set to 1.(dec driver will fill black on the screen)
	DWORD   fld_cnt			:6;		//!<	field count( 0 ~ 59 ), not used in this application
	DWORD   is_cif_left    	:1;		//!< not used, if 1 then left else right position
	DWORD	fld_split		:1;		//!<	Encoded a split even/odd ?, not used in this application

	DWORD	sched			:4;		//!<	temp data for using app, not used in this application
	DWORD	audio			:1;		//!<	temp data for using app, not used in this application
	DWORD   net_skip		:1;		//!< skip the video data of the frame because of flow control. Not used in this application
	DWORD   rec_path		:1;		//!< not used
	DWORD	step_mode		:1;		//!< not used
	DWORD   event_flag		:2;		//!< not used,  event state : EVENT_OFF:0,EVENT_START:1,EVENT_CONTINUE:2
	DWORD   reserved_0		:4;		//!< reserved

	DWORD	if_idx_num;				//!< not used,	0x00 ~ 0xFFFFFFFE( 0xFFFFFFFF is none )
	DWORD	t_idx_num;				//!< not used,	0x00 ~ 0xFFFFFFFE( 0xFFFFFFFF is none )	

	FLOAT gsensor_x;	//!< G sensor value : x-axis
	FLOAT gsensor_y;	//!< G sensor value : y-axis
	FLOAT gsensor_z;	//!< G sensor value : z-axis

	DWORD gps_dir_lati :2;			//!< Direction of latitude
	DWORD gps_dir_longi :2;			//!< Direction of longitude
	DWORD gps_reserved :12;			//!< Reserved
	DWORD reserved  :16;			//!< Reserved

	FLOAT gps_speed;		//!< speed in the unit of knot
	FLOAT gps_latitude;		//!< latitude value from GPS:
	FLOAT gps_longitude;	//!< longitude value from GPS: 
	DWORD gps_utc_time;		//!< UTC time from GPS

#ifdef _EX_HEADER_INFO
	FLOAT gyro_x;
	FLOAT gyro_y;
	FLOAT gyro_z;
#endif

} PICTURE_HEADER_EX, *PPICTURE_HEADER_EX;

typedef struct _FILE_INFO
{
	int nTotalIFrameCount;
	time_t tStartTime;
	time_t tEndTime;
	int nSearchType;
	int nCurIndex;
} ST_FILE_INFO, *LPST_FILE_INFO;

typedef struct _AUDIO_FORMAT_INFO
{
	DWORD dwAudioFormat;
	DWORD dwFrequency;
	DWORD dwBitPerSample;
} ST_AUDIO_FORMAT_INFO, *LPST_AUDIO_FORMAT_INFO;

typedef struct _IMAGE_INFO
{
	//video info
	DWORD dwChannel;
	int nWidth;
	int nHeight;
	time_t tPlayTime;
	DWORD tPlayTimeMillisec;
	LONGLONG llPlayTime;
	int nField;

	//gps info
	FLOAT gps_speed;
	FLOAT gps_longitude;
	FLOAT gps_latitude;
	DWORD gps_utc_time;

	DWORD gps_dir_lati :2;     //
	DWORD gps_dir_longi :2;     //
	DWORD gps_reserved :12;    //
	DWORD reserved  :16;

	//g-sensor
	FLOAT gsensor_x;    //
	FLOAT gsensor_y;       //
	FLOAT gsensor_z;       //

	int nIFrameCount;
	ST_FILE_INFO stFileInfo;

	//user data;
	DWORD dwTotalUserDataCount;
	DWORD dwUserDataSizeAll;
	BYTE *pUserDataAll;
	BOOL IsAvailableToUserDataAll;

	int nCurUserDataPos;
	int nVideoPos;


}ST_IMAGE_INFO, *LPST_IMAGE_INFO;

typedef struct _AUDIO_INFO
{
	time_t tPlayTime;
	DWORD tPlayTimeMillisec;
	LONGLONG llPlayTime;
	DWORD dwAudioBufferSize;
	ST_AUDIO_FORMAT_INFO stAudioFormatInfo;
	int nIndex;
}ST_AUDIO_INFO, *LPST_AUDIO_INFO;

#pragma pack()

#endif // DEF_ARCHIVE_H